<?php
include_once 'ns_include.php';
class User
{
    
    private $property = array();
    
    public static function createNew($Username, $Password, $Firstname, $Lastname, $EmailAddress, $Phone, $UserLevel)
    {
        $U = new User();
        $U->Username($Username);
        $U->Password($Password);
        $U->Firstname($Firstname);
        $U->Lastname($Lastname);
        $U->EmailAddress($EmailAddress);
        $U->Phone($Phone);
        $U->UserLevel($UserLevel);
        
        return $U;
    }

    public static function createFromArray($values)
    {
        if (!is_array($values)) return null;        
        if (count($values) <= 0) return null;
        
        $U = new User();
        $U->FromArray($values);
        return $U;
    }
    
    public function __construct()
    {
        
    }
    
    public function Username($value = null)
    {
        return $this->GetSetData("Username",$value);
    }
    
    public function Password($value = null)
    {
        return $this->GetSetData("Password",$value);
    } 
    
    public function FirstName($value = null)
    {
        return $this->GetSetData("FirstName",$value);
    }
    
    public function LastName($value = null)
    {
        return $this->GetSetData("LastName",$value);
    }
    
    public function LastSeenMessageID($value = null)
    {
        return $this->GetSetData("LastSeenMessageID",$value);
    }
    
    public function AvatarImage($value = null)
    {
        return $this->GetSetData("AvatarImage",$value);
    }

    public function UpdateAvatarImage($postedFile)
    {
        $path_parts = pathinfo($postedFile["name"]);
        $ext = $path_parts['extension'];
        $newFilename = $this->Username() . "-avatar." . $ext;
        $newPathName = self::AvatarImagePath($newFilename);

        @move_uploaded_file($postedFile['tmp_name'], $newPathName);
        if (!file::reallyExists($newPathName)) return FALSE;

        $updated = ns_dao_user::updateUser($this->Username(), "AvatarImage", "");
        $updated = ns_dao_user::updateUser($this->Username(), "AvatarImage", $newFilename);
        
        $this->AvatarImage($newFilename);
        return $updated;
    }

    public static function AvatarImagePath($fileName) {
        return configuration::$APPLICATION_FOLDER . "/content/avatar/" . $fileName;
        //return "../../content/avatar/" . $fileName;
    }

    public function Email($value = null)
    {
        return $this->GetSetData("Email",$value);
    }
    
    public function Phone($value = null)
    {
        return $this->GetSetData("Phone",$value);
    }
    
    public function UserLevel($value = null)
    {
        return $this->GetSetData("UserLevel",$value);
    }
    
    private function GetSetData($property_name,$value = null)
    {
        if (is_null($value) && isset($this->property[$property_name])) return $this->property[$property_name];
        $this->property[$property_name] = $value;
    }   
    
    public function UpdateUserToDatabase()
    {
        $result = ns_dao_user::updateUserByArray($this->property["Username"], $this->property);
        return $result;
    }
    
    /*
     * Populate from an array
     */
    public function FromArray($values)
    {
        if (!is_array($values)) return null;
        
        foreach ($values as $key => $value)
            $this->GetSetData($key,$value);


    }
    
    public function StorableData()
    {
        return $this->property;
    }
    
    public function __toString()
    {
        return print_r($this->property,true);
    }
    
    
}

?>
